package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.SeckillActivities;
import com.example.mall.dto.SeckillActivitiesDto;
import com.example.mall.mapper.SeckillActivitiesMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.SeckillActivitiesQuery;
import com.example.mall.service.ISeckillActivitiesService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 秒杀活动表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-06
 */
@Service
public class SeckillActivitiesServiceImpl extends ServiceImpl<SeckillActivitiesMapper, SeckillActivities> implements ISeckillActivitiesService {
    @Autowired
    private SeckillActivitiesMapper seckillActivitiesMapper;
    @Override
    public PageDTO<SeckillActivitiesDto> queryPage(SeckillActivitiesQuery seckillActivitiesQuery) {
        if (seckillActivitiesQuery == null) {
            seckillActivitiesQuery = new SeckillActivitiesQuery();
        }
        Page<SeckillActivities> page = seckillActivitiesQuery.toMpPageByDefaultCreateTimeAsc();
        Page<SeckillActivities> cartPage = lambdaQuery()
                .like(seckillActivitiesQuery.getActivityId()!= null, SeckillActivities::getActivityId, seckillActivitiesQuery.getActivityId())
                .like(seckillActivitiesQuery.getName()!= null, SeckillActivities::getName, seckillActivitiesQuery.getName())
                .like(seckillActivitiesQuery.getStatus()!= null, SeckillActivities::getStatus, seckillActivitiesQuery.getStatus())
                .apply(seckillActivitiesQuery.getStartTime() != null,
                        "DATE_FORMAT(start_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        seckillActivitiesQuery.getStartTime())
                .apply(seckillActivitiesQuery.getEndTime() != null,
                        "DATE_FORMAT(end_time, '%Y-%m-%d') = DATE_FORMAT({0}, '%Y-%m-%d')",
                        seckillActivitiesQuery.getEndTime())
                .orderByDesc(SeckillActivities::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, SeckillActivitiesDto.class);
    }

    @Override
    public SeckillActivities querySeckillActivitiesById(Long activityId) {
        AssertUtil.isNotNull(activityId, "activityId不能为空");
        return seckillActivitiesMapper.selectById(activityId);
    }

    @Override
    public List<SeckillActivities> querySeckillActivitiesList() {
        return this.list();
    }

    @Override
    public void insert(SeckillActivitiesDto seckillActivitiesDto) {
        SeckillActivities seckillActivities = new SeckillActivities();
        CopyUtils.copyProperties(seckillActivitiesDto, seckillActivities);
        seckillActivities.setActivityId(CodeGenerateUtils.generateUniqueId());
        seckillActivities.setCreateDate(new Date());
        seckillActivities.setUpdateDate(new Date());
        seckillActivities.setStatus(BaseConstants.NOT_USED);
        seckillActivitiesMapper.insert(seckillActivities);
    }

    @Override
    public void update(SeckillActivitiesDto seckillActivitiesDto) {
        AssertUtil.isNotNull(seckillActivitiesDto.getActivityId(), "activityId不能为空");
        SeckillActivities seckillActivities = seckillActivitiesMapper.selectById(seckillActivitiesDto.getActivityId());
        AssertUtil.isNotNull(seckillActivities, "秒杀活动不存在");
        CopyUtils.copyProperties(seckillActivitiesDto, seckillActivities);
        seckillActivities.setUpdateDate(new Date());
        seckillActivitiesMapper.updateById(seckillActivities);
    }

    @Override
    public void delete(Long activityId) {
        AssertUtil.isNotNull(activityId, "activityId不能为空");
        SeckillActivities seckillActivities = seckillActivitiesMapper.selectById(activityId);
        AssertUtil.isNotNull(seckillActivities, "秒杀活动不存在");
        seckillActivities.setStatus(BaseConstants.EXPIRED);
        seckillActivities.setUpdateDate(new Date());
        seckillActivitiesMapper.updateById(seckillActivities);
    }

    @Override
    public void deleteBatch(List<Long> activityIds) {
        AssertUtil.isNotNull(activityIds, "activityIds不能为空");
        List<SeckillActivities> seckillActivities = seckillActivitiesMapper.selectBatchIds(activityIds);
        AssertUtil.isNotNull(seckillActivities, "秒杀活动不存在");
        for (SeckillActivities seckillActivity : seckillActivities) {
            seckillActivity.setStatus(BaseConstants.EXPIRED);
            seckillActivity.setUpdateDate(new Date());
        }
    }
}
